package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.service.CatalogService;
import org.csu.mypetstore.service.LogService;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

public class ViewProductServlet extends HttpServlet {
    private static final String VIEW_PRODUCT = "/WEB-INF/jsp/catalog/Product.jsp";
    private String productId;
    private LogService logService = new LogService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        productId = request.getParameter("productId");
        CatalogService service = new CatalogService();
        Product product = service.getProduct(productId);
        List<Item> itemList = service.getItemListByProduct(productId);
        HttpSession session = request.getSession();

        Account account = (Account)session.getAttribute("account");

        if(account!=null)
        {
            String logInfo = logService.logInfo(" ")  + " 查看产品 " + product;
            logService.insertLogInfo(account.getUsername(), logInfo);
        }

        session.setAttribute("product",product);
//        session.setAttribute("categoryId",product.getCategoryId());
        session.setAttribute("itemList",itemList);
        request.getRequestDispatcher(VIEW_PRODUCT).forward(request,response);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}